package com.movie.controller;

import com.movie.dto.ApiResponse;
import com.movie.dto.CommentDTO;
import com.movie.dto.PageResponse;
import com.movie.service.CommentService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;

import org.springframework.web.bind.annotation.*;


import java.util.List;

@RestController
@RequestMapping("/comments")
@RequiredArgsConstructor
@Tag(name = "评论管理", description = "用户评论的增删改查和审核接口")
public class CommentController {

    private final CommentService commentService;

    @GetMapping
    @Operation(summary = "分页获取评论列表", description = "支持分页的评论列表查询")
    public ApiResponse<PageResponse<CommentDTO>> getComments(
            @Parameter(description = "页码，从0开始") @RequestParam(defaultValue = "0") int page,
            @Parameter(description = "每页大小") @RequestParam(defaultValue = "10") int size) {
        Pageable pageable = PageRequest.of(page, size);
        Page<CommentDTO> comments = commentService.findAll(pageable);

        return ApiResponse.success(PageResponse.of(comments));
    }

    @GetMapping("/movie/{movieId}")
    @Operation(summary = "根据影片获取评论", description = "获取指定影片的所有评论，支持按审核状态筛选")
    public ApiResponse<List<CommentDTO>> getCommentsByMovie(
            @Parameter(description = "影片ID") @PathVariable Long movieId,
            @Parameter(description = "审核状态 (pending, approved, rejected)") @RequestParam(required = false) String status) {
        List<CommentDTO> comments;
        
        if (status != null && !status.isEmpty()) {
            comments = commentService.findByMovieIdAndReviewStatus(movieId, status, Pageable.unpaged()).getContent();
        } else {
            comments = commentService.findByMovieId(movieId, Pageable.unpaged()).getContent();
        }
        
        return ApiResponse.success(comments);
    }

    @GetMapping("/{id}")
    @Operation(summary = "根据ID获取评论", description = "根据评论ID获取具体的评论信息")
    public ApiResponse<CommentDTO> getCommentById(
            @Parameter(description = "评论ID") @PathVariable Long id) {
        CommentDTO comment = commentService.findById(id);
        return ApiResponse.success(comment);
    }

    @PostMapping
    @Operation(summary = "创建评论", description = "用户创建新的评论")
    public ApiResponse<CommentDTO> createComment(
            @Parameter(description = "评论信息") @RequestBody CommentDTO commentDTO) {
        CommentDTO created = commentService.create(commentDTO);
        return ApiResponse.success("评论创建成功", created);
    }

    @PutMapping("/{id}")
    @Operation(summary = "更新评论", description = "根据ID更新评论信息")
    public ApiResponse<CommentDTO> updateComment(
            @Parameter(description = "评论ID") @PathVariable Long id,
            @Parameter(description = "评论信息") @RequestBody CommentDTO commentDTO) {
        CommentDTO updated = commentService.update(id, commentDTO);
        return ApiResponse.success("评论更新成功", updated);
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除评论", description = "根据ID删除评论")
    public ApiResponse<Void> deleteComment(
            @Parameter(description = "评论ID") @PathVariable Long id) {
        commentService.delete(id);
        return ApiResponse.success("评论删除成功", null);
    }


}